/**
 * 
 */
package net.sf.tools.uiconfig.tags.config;

import java.util.List;

import javax.servlet.jsp.JspException;

import net.sf.tools.uiconfig.model.Menu;

/**
 * @author Sabuj Das | sabuj.das@gmail.com
 *
 */
public class MenuConfigTag extends ConfigurationTag {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6887906006519462671L;

	/* (non-Javadoc)
	 * @see net.sf.tools.uiconfig.tags.config.ConfigurationTag#getConfigParam()
	 */
	@Override
	public final String getConfigParam() {
		if (null != getSectionName() && !"".equals(getSectionName().trim())) {
			return "MENU_" + getPageName() + "_" + getSectionName();
		}
		return getPageName();
	}
	
	@Override
	public int doStartTag() throws JspException {
		if(null != getHttpServletRequest().getSession().getAttribute(getConfigParam())){
			pageContext.getServletContext().log("The attribute already present in session: " + getConfigParam());
			return SKIP_BODY;
		}
		if (null != getSectionName() && !"".equals(getSectionName().trim())) {
			// set the list of configurations in the config param
			List<Menu> fieldConfigurations = getConfigurationContext()
					.getConfigurationService().loadMenuConfigurations(
							getHttpServletRequest(), getPageName(), getSectionName());
			getHttpServletRequest().getSession().setAttribute(getConfigParam(),
					fieldConfigurations);
		} else {
			super.doStartTag();
		}

		return SKIP_BODY;
	}
}
